﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Research.DynamicDataDisplay;
using Microsoft.Research.DynamicDataDisplay.DataSources;

namespace Grapher_dot_net
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void InitGraph()
        {
            Microsoft.Research.DynamicDataDisplay.Charts.HorizontalLine hline = new Microsoft.Research.DynamicDataDisplay.Charts.HorizontalLine();
            Microsoft.Research.DynamicDataDisplay.Charts.VerticalLine vline = new Microsoft.Research.DynamicDataDisplay.Charts.VerticalLine();
            vline.Stroke = hline.Stroke = Brushes.Black;
            vline.StrokeThickness = hline.StrokeThickness = 4;
            chartPlotter1.Children.Add(vline);
            chartPlotter1.Children.Add(hline);
            chartPlotter1.Legend.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
         
            string equation = textBox1.Text;
            if (equation.Length == 0)
                return;
            try
            {
                double d = double.Parse(equation);
                chartPlotter1.Children.Add(new Microsoft.Research.DynamicDataDisplay.Charts.HorizontalLine(d));
                
                return;
            }
            catch { }
            Rect range = chartPlotter1.Visible;
            double max = chartPlotter1.Visible.Height;
            double inc = chartPlotter1.Visible.Width/40;
            if (inc < 0.001)
                inc = 0.01;

            Function function = new Function(equation.ToLower());

            List<Point> points = new List<Point>();

            double cent = chartPlotter1.Visible.GetCenter().X;
            double l = -(2*chartPlotter1.Visible.Width) ;
            double r = (2*chartPlotter1.Visible.Width);
           
            for (double x = l ; x <= r; x+=inc)
            {
                double y = function.Evaluate(x);

                if (y > max)
                    max = y;

                points.Add(new Point(x,y));
            }

            var animatedX = new List<double>();
            var animatedY = new List<double>();

            foreach (Point p in points)
            {
                animatedX.Add(p.X);
                animatedY.Add(p.Y);
            }


            // create the X & Y sources
            var _xSource = new EnumerableDataSource<double>(animatedX);
             _xSource.SetXMapping(x => x);
            var _ySource = new EnumerableDataSource<double>(animatedY);
            _ySource.SetYMapping(y => y);

            var graph = new LineGraph(new CompositeDataSource(_xSource, _ySource));
           
            graph.Description = new Microsoft.Research.DynamicDataDisplay.PenDescription("y = " +equation);

            if (comboBox1.SelectedItem as Rectangle != null)
            {
                Brush b = (comboBox1.SelectedItem as Rectangle).Fill;
                graph.LinePen = new Pen(b, 1);
            }


            chartPlotter1.Children.Add(graph);
            
          

           
        }

        private void ExpressionParser()
        {
            
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
           
            InitGraph();
        }
    }
}
